package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardHomePageConfig;
import com.xydtech.ecard.config.service.IEcardHomePageConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子名片首页配置Controller
 *
 * @author yuzhiguo
 * @date 2024-10-25 下午4:15
 */
@Controller
@RequestMapping("/ecard/config/homePage")
public class EcardHomePageConfigController extends BaseController
{
    private String prefix = "ecard/config/homePage";

    @Autowired
    private IEcardHomePageConfigService ecardHomePageConfigService;

    @RequiresPermissions("ecard:config:homePage:view")
    @GetMapping()
    public String homePage()
    {
        return prefix + "/homePage";
    }

    /**
     * 查询电子名片首页配置列表
     */
    @RequiresPermissions("ecard:config:homePage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardHomePageConfig ecardHomePageConfig)
    {
        startPage();
        List<EcardHomePageConfig> list = ecardHomePageConfigService.selectEcardHomePageConfigList(ecardHomePageConfig);
        return getDataTable(list);
    }

    /**
     * 导出电子名片首页配置列表
     */
    @RequiresPermissions("ecard:config:homePage:export")
    @Log(title = "电子名片首页配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardHomePageConfig ecardHomePageConfig)
    {
        List<EcardHomePageConfig> list = ecardHomePageConfigService.selectEcardHomePageConfigList(ecardHomePageConfig);
        ExcelUtil<EcardHomePageConfig> util = new ExcelUtil<EcardHomePageConfig>(EcardHomePageConfig.class);
        return util.exportExcel(list, "电子名片首页配置数据");
    }

    /**
     * 新增电子名片首页配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片首页配置
     */
    @RequiresPermissions("ecard:config:homePage:add")
    @Log(title = "电子名片首页配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardHomePageConfig ecardHomePageConfig)
    {
        EcardHomePageConfig pageConfig = new EcardHomePageConfig();
        pageConfig.setState(1);//启用
        List<EcardHomePageConfig> list = ecardHomePageConfigService.selectEcardHomePageConfigList(pageConfig);
        if (list.size() > 0) {
            return error("已有启用的的首页配置，不可以重复配置！");
        }
        ecardHomePageConfig.setCreatorId(ShiroUtils.getSysUser().getUserId());
        return toAjax(ecardHomePageConfigService.insertEcardHomePageConfig(ecardHomePageConfig));
    }

    /**
     * 修改电子名片首页配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EcardHomePageConfig ecardHomePageConfig = ecardHomePageConfigService.selectEcardHomePageConfigById(id);
        mmap.put("ecardHomePageConfig", ecardHomePageConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片首页配置
     */
    @RequiresPermissions("ecard:config:homePage:edit")
    @Log(title = "电子名片首页配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardHomePageConfig ecardHomePageConfig)
    {
        return toAjax(ecardHomePageConfigService.updateEcardHomePageConfig(ecardHomePageConfig));
    }

    /**
     * 删除电子名片首页配置
     */
    @RequiresPermissions("ecard:config:homePage:remove")
    @Log(title = "电子名片首页配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardHomePageConfigService.deleteEcardHomePageConfigByIds(ids));
    }

    /**
     * 电子名片首页配置状态修改
     * @param ecardHomePageConfig
     * @return
     */
    @RequiresPermissions("ecard:config:homePage:edit")
    @Log(title = "电子名片首页配置状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(EcardHomePageConfig ecardHomePageConfig) {
        return toAjax(ecardHomePageConfigService.updateEcardHomePageConfig(ecardHomePageConfig));
    }
}
